<?php

namespace Lite\Utils;

use \Lite\Loader\Loader;
use \Lite\Exception\ClassException;

abstract class ClassUtils {
	
	/**
	 * @param mixed $value
	 */
	static function isObject($value) {
		return \is_object($value) || 'object' === \gettype($value);
	}
	
	static function getType($value) {
		if( self::isObject($value)) {
			return self::getClassName($value);
		}
		return \gettype($value);
	}
	
	
	/**
	 * @param object $instance
	 */
	static function getClassName($instance) {
		if( self::isObject($instance)) {
			return \get_class($instance);
		}
		return self::getType($instance);
	}
	
	/**
	 * @param object $instance
	 */
	static function getSimpleClassName($instance) {
		$className = self::getClassName($instance);
		return StringUtils::subStringAfter( $className, '\\');
	}
	
	/**
	 * @param string $className
	 * @return object
	 */
	static function newInstance($className, array $args=null) {
		
		$reflectionClass = new \ReflectionClass($className);
       
		if( null !== $args ) {
			return $reflectionClass->newInstanceArgs( $args ); 
		}
		
		return $reflectionClass->newInstance(); 
	}

	/**
	 * @param string $className
	 */
	static function classExists($className, $autoload=true) {
		return class_exists($className, $autoload) || interface_exists($className, $autoload);
	}
	
	/**
	 * @param string $className
	 */
	static function classAlreadyLoaded($className) {
		return self::classExists($className, false);
	}
	
	/**
	 * Intenta carga una clase
	 * 
	 * @param string $className
	 * @return boolean
	 */
	static function loadClass($className) { 
		if( self::classAlreadyLoaded($className)) {
			return true;
		}
		
		$loader = Loader::getInstance();
		return $loader->autoload($className);
	}
}